public class CinemaData {
    // 影院厅信息
    public static final String[] HALLS = {"黄金厅", "白银厅", "翡翠厅", "钻石厅", "宝石厅"};
    public static final int[] HALL_SEATS = {50, 40, 30, 25, 20}; // 各厅座位数

    // 数据存储数组
    private static User[] users = new User[10]; // 最大10个用户
    private static Movie[] movies = new Movie[20]; // 最大20部电影
    private static Schedule[] schedules = new Schedule[50]; // 最大50个场次

    // 记录当前数据数量
    private static int userCount = 0;
    private static int movieCount = 0;
    private static int scheduleCount = 0;

    // 修改静态初始化默认数据中的管理员密码
    static {
        // 添加默认用户，将admin密码改为ynuinfo#777
        addUser(new User("admin", "ynuinfo#777", "admin"));
        addUser(new User("manager", "manager123", "manager"));
        addUser(new User("front", "front123", "front"));

    // 添加默认电影
        addMovie(new Movie("M001", "流浪地球", "125min", "科幻", 50.0));
        addMovie(new Movie("M002", "满江红", "159min", "悬疑", 45.0));

        // 添加默认场次
        addSchedule(new Schedule("S001", "M001", "黄金厅", "2023-10-01", "19:00", 50));
        addSchedule(new Schedule("S002", "M002", "白银厅", "2023-10-01", "20:30", 40));
    }

    // 用户相关操作
    public static boolean addUser(User user) {
        if (userCount < users.length) {
            users[userCount++] = user;
            return true;
        }
        return false;
    }

    public static User findUserByUsername(String username) {
        for (int i = 0; i < userCount; i++) {
            if (users[i].getUsername().equals(username)) {
                return users[i];
            }
        }
        return null;
    }

    public static User[] getAllUsers() {
        User[] result = new User[userCount];
        System.arraycopy(users, 0, result, 0, userCount);
        return result;
    }

    public static boolean deleteUser(String username) {
        for (int i = 0; i < userCount; i++) {
            if (users[i].getUsername().equals(username)) {
                // 移除此用户，后面的用户前移
                for (int j = i; j < userCount - 1; j++) {
                    users[j] = users[j + 1];
                }
                users[userCount - 1] = null;
                userCount--;
                return true;
            }
        }
        return false;
    }

    // 电影相关操作
    public static boolean addMovie(Movie movie) {
        if (movieCount < movies.length) {
            movies[movieCount++] = movie;
            return true;
        }
        return false;
    }

    public static Movie findMovieById(String id) {
        for (int i = 0; i < movieCount; i++) {
            if (movies[i].getId().equals(id)) {
                return movies[i];
            }
        }
        return null;
    }

    public static Movie[] getAllMovies() {
        Movie[] result = new Movie[movieCount];
        System.arraycopy(movies, 0, result, 0, movieCount);
        return result;
    }

    public static boolean deleteMovie(String id) {
        for (int i = 0; i < movieCount; i++) {
            if (movies[i].getId().equals(id)) {
                // 移除此电影，后面的电影前移
                for (int j = i; j < movieCount - 1; j++) {
                    movies[j] = movies[j + 1];
                }
                movies[movieCount - 1] = null;
                movieCount--;
                return true;
            }
        }
        return false;
    }

    // 场次相关操作
    public static boolean addSchedule(Schedule schedule) {
        if (scheduleCount < schedules.length) {
            schedules[scheduleCount++] = schedule;
            return true;
        }
        return false;
    }

    public static Schedule findScheduleById(String id) {
        for (int i = 0; i < scheduleCount; i++) {
            if (schedules[i].getId().equals(id)) {
                return schedules[i];
            }
        }
        return null;
    }

    public static Schedule[] getSchedulesByMovieId(String movieId) {
        // 先统计数量
        int count = 0;
        for (int i = 0; i < scheduleCount; i++) {
            if (schedules[i].getMovieId().equals(movieId)) {
                count++;
            }
        }

        // 再填充结果
        Schedule[] result = new Schedule[count];
        int index = 0;
        for (int i = 0; i < scheduleCount; i++) {
            if (schedules[i].getMovieId().equals(movieId)) {
                result[index++] = schedules[i];
            }
        }
        return result;
    }

    public static Schedule[] getAllSchedules() {
        Schedule[] result = new Schedule[scheduleCount];
        System.arraycopy(schedules, 0, result, 0, scheduleCount);
        return result;
    }

    public static boolean deleteSchedule(String id) {
        for (int i = 0; i < scheduleCount; i++) {
            if (schedules[i].getId().equals(id)) {
                // 移除此场次，后面的场次前移
                for (int j = i; j < scheduleCount - 1; j++) {
                    schedules[j] = schedules[j + 1];
                }
                schedules[scheduleCount - 1] = null;
                scheduleCount--;
                return true;
            }
        }
        return false;
    }
}
